package com.sxhalo.fsms.controller.api;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.domain.FactArea;
import com.sxhalo.fsms.log.SysControllerLog;
import com.sxhalo.fsms.service.IFactAreaService;
import com.sxhalo.fsms.util.ApiResult;
import com.sxhalo.fsms.util.PageData;
import com.sxhalo.fsms.util.TreeData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/api")
public class FactAreaController extends BaseController {

	@Autowired
	private IFactAreaService areaService;

	@ResponseBody
	@RequestMapping(value = "/area", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult findArea(Integer current, Integer parentId, Integer pageSize, HttpServletRequest req) {
		String where = "a.delete_flag=0";
		String order = " parent_id asc,area_id asc";
		if (!isBlank(req.getParameter("areaName"))) {
			where += " and a.area_name like '%" + req.getParameter("areaName").trim() + "%'";
		}
		if (!isBlank(req.getParameter("areaDesc"))) {
			where += " and a.area_desc like '%" + req.getParameter("areaDesc").trim() + "%'";
		}
		if (!isBlank(req.getParameter("useState"))) {
			where += " and a.use_state=" + req.getParameter("useState");
		}
		if (parentId == null || parentId == 0) {
			return findList(current, pageSize, where, order, areaService);
		} else {
			Integer start = pageSize * (current - 1);
			Integer count = areaService.getChildCount(where, parentId);
			List<FactArea> list = areaService.findChildPagerList(start, pageSize, where, order, parentId);
			PageData page = new PageData(current, pageSize, count, list);
			return ApiResult.retOK(req, page);
		}

	}





	@ResponseBody
	@RequestMapping(value = "/area/list", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult findAreaList(Integer current, Integer parentId, Integer pageSize, HttpServletRequest req) {
		String where = "a.delete_flag=0";
		String order = " parent_id asc,area_id asc";
		if (!isBlank(req.getParameter("areaName"))) {
			where += " and a.area_name like '%" + req.getParameter("areaName").trim() + "%'";
		}
		if (!isBlank(req.getParameter("areaDesc"))) {
			where += " and a.area_desc like '%" + req.getParameter("areaDesc").trim() + "%'";
		}
		if (!isBlank(req.getParameter("useState"))) {
			where += " and a.use_state=" + req.getParameter("useState");
		}
		if (parentId == null || parentId == 0) {
			where += " and a.parent_id=" + parentId;
		}
		List<FactArea> list = areaService.findWhereList(where, order);
		return ApiResult.retOK(req, list);

	}

	@ResponseBody
	@SysControllerLog(operationName = "添加区域", modelName = "区域管理")
	@RequestMapping(value = "/area", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ApiResult saveArea( FactArea record, String method, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		record.setDeleteFlag(0);
		record.setCreateTime(nowtime);
		areaService.insertSelective(record);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "更新区域", modelName = "区域管理")
	@RequestMapping(value = "/area", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
	public ApiResult updateArea(@RequestBody FactArea record, String method, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		areaService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "删除区域", modelName = "区域管理")
	@RequestMapping(value = "/area", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	public ApiResult delArea(Integer areaId, String method, HttpServletRequest req) {
		FactArea record = new FactArea();
		record.setAreaId(areaId);
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setDeleteFlag(1);
		record.setUpdateTime(nowtime);
		areaService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@RequestMapping(value = "/area/tree", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult findTree(String type, Integer areaId, HttpServletRequest req) {
		List<TreeData> treeData = new ArrayList<>();

		if (type != null && "root".equals(type)) {
			TreeData item = new TreeData();
			item.setTitle("根目录");
			item.setValue("0");
			item.setKey("0");
			item.setChildren(findTreeData(0, areaId));
			treeData.add(item);
		} else {
			treeData = findTreeData(0, areaId);
		}
		return ApiResult.retOK(req, treeData);
	}

	private List<TreeData> findTreeData(Integer parentId, Integer areaId) {
		List<TreeData> treeData = new ArrayList<TreeData>();

		List<FactArea> list = areaService.findWhereList("delete_flag=0  and parent_id=" + parentId, " area_id ");
		for (FactArea department : list) {
			TreeData item = new TreeData();
			item.setTitle(department.getAreaName());
			item.setKey(department.getAreaId().toString());
			item.setValue(department.getAreaId().toString());
			if (areaId != null && areaId == department.getAreaId()) {
				item.setDisabled(true);
			}

			List<TreeData> children = findTreeData(department.getAreaId(), areaId);
			if (children.size() > 0) {
				item.setChildren(children);
			}
			treeData.add(item);
		}
		return treeData;
	}
}
